package com.czl.module_service.view.approvalFlow

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.czl.base.data.bean.tengyun.TaskUserBean
import com.czl.module_service.R
import com.czl.module_service.databinding.LayoutApprovalFlowBinding

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
class ApprovalFlowLayout(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    FrameLayout(context, attrs, defStyleAttr) {
    val binding : LayoutApprovalFlowBinding =
        LayoutApprovalFlowBinding.inflate(LayoutInflater.from(context), this, true)
    val mAdapter: ApprovalFlowAdapter by lazy {
        ApprovalFlowAdapter()
    }

    init {
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
            isNestedScrollingEnabled = false
        }
    }

    open fun addHeaderView(view: View) {
        binding.layoutTop.addView(view)
    }

    fun getTopLayout() : ViewGroup {
        return binding.layoutTop
    }

    fun getBottomLayout() : ViewGroup {
        return binding.layoutBottom
    }

    open fun addFooterView(view: View) {
        binding.layoutBottom.addView(view)
    }

    var mData: List<TaskUserBean> = mutableListOf()
        set(value) {
            field = value
            val list = mutableListOf<TaskUserBean>()
            field.forEach {
                it.subheadName = "发起申请"
                it.subheadTitle = it.tname
                it.imageRes = R.drawable.shape_bg_circle
                it.operationImageRes = R.drawable.ic_spl_gou
                list.add(it)
                it.list?.forEach { it2 ->
                    if (it2.taskType != null) {
                        if (it2.taskType == 0) {
                            it2.imageRes = R.drawable.shape_sound_circle
                            it2.subheadName = "抄送" + it2.taskUserList!!.size + "人"
                            it2.operationImageRes = R.drawable.ic_spl_dai
                            list.add(it2)
                        } else {
                            it2.taskUserList?.forEach { it3 ->
                                var statusName : String? = null
                                if (it3.status == 1) statusName = "审批通过" else if (it3.status == 2) statusName = "待审批" else if (it3.status == 3) statusName = "审批驳回" else if (it3.status == 5) statusName = "已撤回"
                                it3.subheadName = it3.tname + "(" + statusName + ")"
                                if (it3.taskDesc != null) {
                                    it3.subheadTitle = it3.taskDesc
                                }
                                it3.imageRes = R.drawable.shape_bg_circle
                                if (it3.status == 2) {
                                    it3.operationImageRes = R.drawable.ic_icn_spl_shen
                                } else {
                                    it3.operationImageRes = R.drawable.ic_spl_gou
                                }
                                list.add(it3)
                            }
                        }
                    }
                }
            }
            mAdapter.addData(list)
        }
}